package com.itrus.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class KeyStoreUtil
{
  private static final HashMap AVAILABLE_TYPES = new HashMap();

  static
  {
    if (Security.getProvider("BC") == null)
      Security.addProvider(new BouncyCastleProvider());
  }

  public static String ExportPriv(String paramString, File paramFile, char[] paramArrayOfChar)
    throws Exception
  {
    KeyStore localKeyStore = KeyStore.getInstance("JKS");
    localKeyStore.load(new FileInputStream(paramFile), paramArrayOfChar);
    PrivateKey localPrivateKey = getPrivateKey(localKeyStore, paramString, paramArrayOfChar).getPrivate();
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("-----BEGIN RSA PRIVATE KEY-----\n");
    localStringBuffer.append(new String(Base64.encode(localPrivateKey.getEncoded(), true)));
    localStringBuffer.append("\n-----END RSA PRIVATE KEY-----");
    return localStringBuffer.toString();
  }

  public static void JKSToPKCS12(String paramString1, char[] paramArrayOfChar1, String paramString2, char[] paramArrayOfChar2, String paramString3, char[] paramArrayOfChar3)
    throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException
  {
    KeyStore localKeyStore1 = KeyStore.getInstance("JKS");
    FileInputStream localFileInputStream = new FileInputStream(paramString1);
    localKeyStore1.load(localFileInputStream, paramArrayOfChar1);
    localFileInputStream.close();
    KeyStore localKeyStore2 = KeyStore.getInstance("PKCS12");
    localKeyStore2.load(null, paramArrayOfChar3);
    Enumeration localEnumeration = localKeyStore1.aliases();
    while (true)
    {
      if (!localEnumeration.hasMoreElements())
      {
        FileOutputStream localFileOutputStream = new FileOutputStream(paramString3);
        localKeyStore2.store(localFileOutputStream, paramArrayOfChar3);
        localFileOutputStream.close();
        return;
      }
      String str = (String)localEnumeration.nextElement();
      if ((localKeyStore1.isKeyEntry(str)) && (str.equals(paramString2)))
        localKeyStore2.setKeyEntry(paramString2, localKeyStore1.getKey(str, paramArrayOfChar2), paramArrayOfChar3, localKeyStore1.getCertificateChain(str));
    }
  }

  public static KeyStore createKeyStore(KeyStoreType paramKeyStoreType)
    throws CryptoException, IOException
  {
    try
    {
      KeyStore localKeyStore = getKeyStoreImpl(paramKeyStoreType);
      localKeyStore.load(null, null);
      return localKeyStore;
    }
    catch (GeneralSecurityException localGeneralSecurityException)
    {
      throw new CryptoException(localGeneralSecurityException);
    }
  }

  public static SubjectKeyIdentifier createSubjectKeyId(PublicKey paramPublicKey)
  {
    try
    {
      SubjectKeyIdentifier localSubjectKeyIdentifier = new SubjectKeyIdentifier(new SubjectPublicKeyInfo((ASN1Sequence)new ASN1InputStream(new ByteArrayInputStream(paramPublicKey.getEncoded())).readObject()));
      return localSubjectKeyIdentifier;
    }
    catch (Exception localException)
    {
    }
    throw new RuntimeException("error creating key");
  }

  // ERROR //
  public static String genCertReq(File paramFile1, File paramFile2, char[] paramArrayOfChar, KeyStoreType paramKeyStoreType)
    throws CryptoException, java.io.FileNotFoundException
  {
    // Byte code:
    //   0: aload_3
    //   1: invokestatic 163	com/itrus/util/KeyStoreUtil:getKeyStoreImpl	(Lcom/itrus/util/KeyStoreType;)Ljava/security/KeyStore;
    //   4: astore 5
    //   6: new 45	java/io/FileInputStream
    //   9: dup
    //   10: aload_0
    //   11: invokespecial 48	java/io/FileInputStream:<init>	(Ljava/io/File;)V
    //   14: astore 6
    //   16: aload 5
    //   18: aload 6
    //   20: aload_2
    //   21: invokevirtual 52	java/security/KeyStore:load	(Ljava/io/InputStream;[C)V
    //   24: aload 5
    //   26: invokevirtual 116	java/security/KeyStore:aliases	()Ljava/util/Enumeration;
    //   29: astore 12
    //   31: aload 12
    //   33: invokeinterface 122 1 0
    //   38: istore 13
    //   40: aconst_null
    //   41: astore 14
    //   43: iload 13
    //   45: ifne +28 -> 73
    //   48: aload 14
    //   50: ifnull +206 -> 256
    //   53: aload 6
    //   55: invokevirtual 110	java/io/FileInputStream:close	()V
    //   58: aload 14
    //   60: areturn
    //   61: astore 4
    //   63: new 158	com/itrus/util/CryptoException
    //   66: dup
    //   67: aload 4
    //   69: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   72: athrow
    //   73: aload 12
    //   75: invokeinterface 134 1 0
    //   80: invokevirtual 207	java/lang/Object:toString	()Ljava/lang/String;
    //   83: astore 15
    //   85: aload 5
    //   87: aload 15
    //   89: invokevirtual 138	java/security/KeyStore:isKeyEntry	(Ljava/lang/String;)Z
    //   92: ifeq -61 -> 31
    //   95: aload 5
    //   97: aload 15
    //   99: invokevirtual 211	java/security/KeyStore:getCertificate	(Ljava/lang/String;)Ljava/security/cert/Certificate;
    //   102: checkcast 213	java/security/cert/X509Certificate
    //   105: astore 16
    //   107: aload 16
    //   109: invokestatic 218	com/itrus/cert/X509Certificate:getInstance	(Ljava/security/cert/X509Certificate;)Lcom/itrus/cert/X509Certificate;
    //   112: astore 17
    //   114: aload 5
    //   116: aload 15
    //   118: aload_2
    //   119: invokevirtual 146	java/security/KeyStore:getKey	(Ljava/lang/String;[C)Ljava/security/Key;
    //   122: checkcast 75	java/security/PrivateKey
    //   125: astore 18
    //   127: aload 16
    //   129: invokevirtual 222	java/security/cert/X509Certificate:getPublicKey	()Ljava/security/PublicKey;
    //   132: astore 19
    //   134: new 224	com/itrus/cert/PKCS10CertificationRequest
    //   137: dup
    //   138: ldc 226
    //   140: aload 17
    //   142: invokevirtual 230	com/itrus/cert/X509Certificate:getSubjectX500Principal	()Ljavax/security/auth/x500/X500Principal;
    //   145: aload 19
    //   147: aconst_null
    //   148: aload 18
    //   150: ldc 10
    //   152: invokespecial 233	com/itrus/cert/PKCS10CertificationRequest:<init>	(Ljava/lang/String;Ljavax/security/auth/x500/X500Principal;Ljava/security/PublicKey;Lorg/bouncycastle/asn1/ASN1Set;Ljava/security/PrivateKey;Ljava/lang/String;)V
    //   155: invokevirtual 234	com/itrus/cert/PKCS10CertificationRequest:getEncoded	()[B
    //   158: astore 20
    //   160: new 124	java/io/FileOutputStream
    //   163: dup
    //   164: aload_1
    //   165: invokespecial 235	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
    //   168: astore 21
    //   170: new 237	java/io/OutputStreamWriter
    //   173: dup
    //   174: aload 21
    //   176: invokespecial 240	java/io/OutputStreamWriter:<init>	(Ljava/io/OutputStream;)V
    //   179: astore 22
    //   181: aload 20
    //   183: invokestatic 243	com/itrus/util/Base64:encode	([B)Ljava/lang/String;
    //   186: astore 14
    //   188: aload 22
    //   190: ldc 245
    //   192: invokevirtual 248	java/io/OutputStreamWriter:write	(Ljava/lang/String;)V
    //   195: aload 22
    //   197: aload 14
    //   199: invokevirtual 248	java/io/OutputStreamWriter:write	(Ljava/lang/String;)V
    //   202: aload 22
    //   204: ldc 250
    //   206: invokevirtual 248	java/io/OutputStreamWriter:write	(Ljava/lang/String;)V
    //   209: aload 22
    //   211: invokevirtual 253	java/io/OutputStreamWriter:flush	()V
    //   214: goto -166 -> 48
    //   217: astore 11
    //   219: new 158	com/itrus/util/CryptoException
    //   222: dup
    //   223: aload 11
    //   225: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   228: athrow
    //   229: astore 8
    //   231: aload 6
    //   233: invokevirtual 110	java/io/FileInputStream:close	()V
    //   236: aload 8
    //   238: athrow
    //   239: astore 10
    //   241: aload 10
    //   243: athrow
    //   244: astore 7
    //   246: new 158	com/itrus/util/CryptoException
    //   249: dup
    //   250: aload 7
    //   252: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   255: athrow
    //   256: aload 6
    //   258: invokevirtual 110	java/io/FileInputStream:close	()V
    //   261: aload 14
    //   263: areturn
    //   264: astore 23
    //   266: aload 14
    //   268: areturn
    //   269: astore 24
    //   271: aload 14
    //   273: areturn
    //   274: astore 9
    //   276: goto -40 -> 236
    //
    // Exception table:
    //   from	to	target	type
    //   0	6	61	java/security/KeyStoreException
    //   16	31	217	java/security/GeneralSecurityException
    //   31	40	217	java/security/GeneralSecurityException
    //   73	214	217	java/security/GeneralSecurityException
    //   16	31	229	finally
    //   31	40	229	finally
    //   73	214	229	finally
    //   219	229	229	finally
    //   241	244	229	finally
    //   246	256	229	finally
    //   16	31	239	java/io/FileNotFoundException
    //   31	40	239	java/io/FileNotFoundException
    //   73	214	239	java/io/FileNotFoundException
    //   16	31	244	java/io/IOException
    //   31	40	244	java/io/IOException
    //   73	214	244	java/io/IOException
    //   256	261	264	java/io/IOException
    //   53	58	269	java/io/IOException
    //   231	236	274	java/io/IOException
  }

  public static KeyPair genKeys(int paramInt)
    throws NoSuchAlgorithmException, NoSuchProviderException
  {
    KeyPairGenerator localKeyPairGenerator = KeyPairGenerator.getInstance("RSA", "SunJSSE");
    localKeyPairGenerator.initialize(paramInt);
    return localKeyPairGenerator.generateKeyPair();
  }

  public static KeyStoreType[] getAvailableTypes()
  {
    KeyStoreType[] arrayOfKeyStoreType = KeyStoreType.getKnownTypes();
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    int j = arrayOfKeyStoreType.length;
    while (true)
    {
      if (i >= j)
        return (KeyStoreType[])localArrayList.toArray(new KeyStoreType[localArrayList.size()]);
      if (isAvailable(arrayOfKeyStoreType[i]))
        localArrayList.add(arrayOfKeyStoreType[i]);
      i++;
    }
  }

  public static Certificate[] getCertChain(KeyStore paramKeyStore, String paramString)
    throws KeyStoreException
  {
    Certificate[] arrayOfCertificate1 = paramKeyStore.getCertificateChain(paramString);
    if (arrayOfCertificate1.length < 1);
    while (arrayOfCertificate1.length > 0)
      return arrayOfCertificate1;
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    int j;
    label40: Certificate[] arrayOfCertificate3;
    if (i >= arrayOfCertificate1.length)
    {
      j = 0;
      if (j == 0)
        break label83;
      arrayOfCertificate3 = new Certificate[localArrayList.size()];
    }
    for (int m = 0; ; m++)
    {
      if (m >= arrayOfCertificate3.length)
      {
        return arrayOfCertificate3;
        localArrayList.add(arrayOfCertificate1[i]);
        i++;
        break;
        label83: Certificate[] arrayOfCertificate2 = paramKeyStore.getCertificateChain(CertUtils.getPartFromDN(CertUtils.getIssuerDN((X509Certificate)localArrayList.get(-1 + localArrayList.size())), "CN"));
        if (arrayOfCertificate2 == null)
        {
          j = 1;
          break label40;
        }
        if (arrayOfCertificate2.length == 0)
          j = 1;
        for (int k = 0; k < arrayOfCertificate2.length; k++)
        {
          localArrayList.add(arrayOfCertificate2[k]);
          if (CertUtils.isSelfSigned((X509Certificate)arrayOfCertificate2[k]))
            j = 1;
        }
        break label40;
      }
      arrayOfCertificate3[m] = ((X509Certificate)localArrayList.get(m));
    }
  }

  private static KeyStore getKeyStoreImpl(KeyStoreType paramKeyStoreType)
    throws KeyStoreException
  {
    KeyStoreType localKeyStoreType = KeyStoreType.PKCS12;
    Object localObject = null;
    if (paramKeyStoreType == localKeyStoreType);
    try
    {
      KeyStore localKeyStore2 = KeyStore.getInstance(paramKeyStoreType.toString(), "BC");
      localObject = localKeyStore2;
      if (localObject == null);
      try
      {
        KeyStore localKeyStore1 = KeyStore.getInstance(paramKeyStoreType.toString());
        localObject = localKeyStore1;
        AVAILABLE_TYPES.put(paramKeyStoreType, Boolean.TRUE);
        return localObject;
      }
      catch (KeyStoreException localKeyStoreException)
      {
        AVAILABLE_TYPES.put(paramKeyStoreType, Boolean.FALSE);
        throw localKeyStoreException;
      }
    }
    catch (NoSuchProviderException localNoSuchProviderException)
    {
      while (true)
        localObject = null;
    }
  }

  public static KeyPair getPrivateKey(KeyStore paramKeyStore, String paramString, char[] paramArrayOfChar)
  {
    try
    {
      Key localKey = paramKeyStore.getKey(paramString, paramArrayOfChar);
      if ((localKey instanceof PrivateKey))
      {
        KeyPair localKeyPair = new KeyPair(paramKeyStore.getCertificate(paramString).getPublicKey(), (PrivateKey)localKey);
        return localKeyPair;
      }
    }
    catch (KeyStoreException localKeyStoreException)
    {
      return null;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      break label43;
    }
    catch (UnrecoverableKeyException localUnrecoverableKeyException)
    {
      label43: break label43;
    }
  }

  public static boolean isAvailable(KeyStoreType paramKeyStoreType)
  {
    Boolean localBoolean = (Boolean)AVAILABLE_TYPES.get(paramKeyStoreType);
    if (localBoolean != null)
      return localBoolean.booleanValue();
    try
    {
      getKeyStoreImpl(paramKeyStoreType);
      label25: return ((Boolean)AVAILABLE_TYPES.get(paramKeyStoreType)).booleanValue();
    }
    catch (KeyStoreException localKeyStoreException)
    {
      break label25;
    }
  }

  public static boolean isWindowsKeyStore(String paramString)
  {
    return (paramString.equalsIgnoreCase("Windows-MY")) || (paramString.equalsIgnoreCase("Windows-ROOT"));
  }

  // ERROR //
  public static KeyStore loadKeyStore(File paramFile, char[] paramArrayOfChar, KeyStoreType paramKeyStoreType)
    throws CryptoException, java.io.FileNotFoundException
  {
    // Byte code:
    //   0: aload_2
    //   1: invokestatic 163	com/itrus/util/KeyStoreUtil:getKeyStoreImpl	(Lcom/itrus/util/KeyStoreType;)Ljava/security/KeyStore;
    //   4: astore 4
    //   6: new 45	java/io/FileInputStream
    //   9: dup
    //   10: aload_0
    //   11: invokespecial 48	java/io/FileInputStream:<init>	(Ljava/io/File;)V
    //   14: astore 5
    //   16: aload 4
    //   18: aload 5
    //   20: aload_1
    //   21: invokevirtual 52	java/security/KeyStore:load	(Ljava/io/InputStream;[C)V
    //   24: aload 5
    //   26: invokevirtual 110	java/io/FileInputStream:close	()V
    //   29: aload 4
    //   31: areturn
    //   32: astore_3
    //   33: new 158	com/itrus/util/CryptoException
    //   36: dup
    //   37: aload_3
    //   38: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   41: athrow
    //   42: astore 10
    //   44: new 158	com/itrus/util/CryptoException
    //   47: dup
    //   48: aload 10
    //   50: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   53: athrow
    //   54: astore 7
    //   56: aload 5
    //   58: invokevirtual 110	java/io/FileInputStream:close	()V
    //   61: aload 7
    //   63: athrow
    //   64: astore 9
    //   66: aload 9
    //   68: athrow
    //   69: astore 6
    //   71: new 158	com/itrus/util/CryptoException
    //   74: dup
    //   75: aload 6
    //   77: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   80: athrow
    //   81: astore 8
    //   83: goto -22 -> 61
    //   86: astore 11
    //   88: aload 4
    //   90: areturn
    //
    // Exception table:
    //   from	to	target	type
    //   0	6	32	java/security/KeyStoreException
    //   16	24	42	java/security/GeneralSecurityException
    //   16	24	54	finally
    //   44	54	54	finally
    //   66	69	54	finally
    //   71	81	54	finally
    //   16	24	64	java/io/FileNotFoundException
    //   16	24	69	java/io/IOException
    //   56	61	81	java/io/IOException
    //   24	29	86	java/io/IOException
  }

  public static KeyStore loadKeyStore(String paramString, char[] paramArrayOfChar)
    throws CryptoException
  {
    try
    {
      if (Security.getProvider(paramString) == null)
        throw new CryptoException(new String("Provider not found: " + paramString));
    }
    catch (GeneralSecurityException localGeneralSecurityException)
    {
      throw new CryptoException(localGeneralSecurityException);
    }
    KeyStore localKeyStore = KeyStore.getInstance(KeyStoreType.PKCS11.toString(), paramString);
    try
    {
      localKeyStore.load(null, paramArrayOfChar);
      return localKeyStore;
    }
    catch (Exception localException)
    {
      throw new CryptoException(localException);
    }
  }

  public static String niceStoreTypeName(String paramString)
  {
    if (paramString.equalsIgnoreCase("Windows-MY"))
      return "Windows-MY";
    if (paramString.equalsIgnoreCase("Windows-ROOT"))
      return "Windows-ROOT";
    return paramString.toUpperCase();
  }

  // ERROR //
  public static KeyStore saveKeyStore(KeyStore paramKeyStore, File paramFile, char[] paramArrayOfChar)
    throws CryptoException, IOException
  {
    // Byte code:
    //   0: new 124	java/io/FileOutputStream
    //   3: dup
    //   4: aload_1
    //   5: invokespecial 235	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
    //   8: astore_3
    //   9: aload_0
    //   10: aload_3
    //   11: aload_2
    //   12: invokevirtual 129	java/security/KeyStore:store	(Ljava/io/OutputStream;[C)V
    //   15: aload_3
    //   16: invokevirtual 130	java/io/FileOutputStream:close	()V
    //   19: getstatic 389	com/itrus/util/KeyStoreType:GKR	Lcom/itrus/util/KeyStoreType;
    //   22: aload_0
    //   23: invokevirtual 392	java/security/KeyStore:getType	()Ljava/lang/String;
    //   26: invokestatic 395	com/itrus/util/KeyStoreType:getInstance	(Ljava/lang/String;)Lcom/itrus/util/KeyStoreType;
    //   29: invokevirtual 396	java/lang/Object:equals	(Ljava/lang/Object;)Z
    //   32: ifeq +12 -> 44
    //   35: aload_1
    //   36: aload_2
    //   37: getstatic 389	com/itrus/util/KeyStoreType:GKR	Lcom/itrus/util/KeyStoreType;
    //   40: invokestatic 398	com/itrus/util/KeyStoreUtil:loadKeyStore	(Ljava/io/File;[CLcom/itrus/util/KeyStoreType;)Ljava/security/KeyStore;
    //   43: astore_0
    //   44: aload_0
    //   45: areturn
    //   46: astore 6
    //   48: new 158	com/itrus/util/CryptoException
    //   51: dup
    //   52: aload 6
    //   54: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   57: athrow
    //   58: astore 5
    //   60: aload_3
    //   61: invokevirtual 130	java/io/FileOutputStream:close	()V
    //   64: aload 5
    //   66: athrow
    //   67: astore 4
    //   69: new 158	com/itrus/util/CryptoException
    //   72: dup
    //   73: aload 4
    //   75: invokespecial 166	com/itrus/util/CryptoException:<init>	(Ljava/lang/Throwable;)V
    //   78: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   9	15	46	java/io/IOException
    //   9	15	58	finally
    //   48	58	58	finally
    //   69	79	58	finally
    //   9	15	67	java/security/GeneralSecurityException
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.itrus.util.KeyStoreUtil
 * JD-Core Version:    0.6.2
 */